package com.xiaoshuidi.cloud.module.report.service.bigdata;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.xiaoshuidi.cloud.module.report.constant.TotalTransactionDetailsTypeEnum;
import com.xiaoshuidi.cloud.module.report.controller.bigdata.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.bigdata.OverviewTransactionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class OverviewTransactionImpl implements OverviewTransactionService {

    private final OverviewTransactionMapper overviewTransactionMapper;


    @Override
    public PaymentOutflowRatioVO getPaymentOutflowData(Date startTime, Date endTime, Long aptId) {
        List<PaymentOutflowVO> paymentOutflowData =
                overviewTransactionMapper.getPaymentOutflowData(startTime != null ? DateUtil.beginOfDay(startTime) :
                        null, endTime != null ? DateUtil.endOfDay(endTime) : null, aptId);
        PaymentOutflowRatioVO vo = new PaymentOutflowRatioVO();
        if (CollectionUtil.isNotEmpty(paymentOutflowData)) {
            paymentOutflowData.forEach(f -> {
                switch (f.getBizType()) {
                    case OWNER_ORDER:
                        vo.setOwnerOrder(f.getAmount());
                        vo.setOwnerOrderCount(f.getAmountCount());
                        break;
                    case SETTLEMENT_BILL:
                        vo.setSettlementBill(f.getAmount());
                        vo.setSettlementCount(f.getAmountCount());
                        break;
                    case ACCOUNT_RECHARGE:
                        vo.setAccountRecharge(f.getAmount().add(vo.getAccountRecharge()));
                        vo.setAccountRechargeCount(vo.getAccountRechargeCount() + f.getAmountCount());
                        break;
                    case WITHDRAW:
                        //房屋钱包提现
                        vo.setAccountRecharge(f.getAmount().add(vo.getAccountRecharge()));
                        vo.setAccountRechargeCount(vo.getAccountRechargeCount() + f.getAmountCount());
                        break;
                    default:
                        break;
                }
            });
            vo.setTotalAmount(vo.getSettlementBill().add(vo.getOwnerOrder()).add(vo.getAccountRecharge()).add(vo.getHouseRecharge()));
        }

        return vo;
    }

    @Override
    public List<YearMonthRevenueVO> getYearMonthRevenueStatistics() {
        List<String> yearMonthList = getYearMonthList();
        List<YearMonthRevenueVO> yearMonthRevenueStatistics =
                overviewTransactionMapper.getYearMonthRevenueStatistics(yearMonthList.get(0),
                        yearMonthList.get(yearMonthList.size()-1));
        List<YearMonthRevenueVO> collect =
                yearMonthList.stream().map(f -> YearMonthRevenueVO.builder().dateYearMonth(f).totalAmount(BigDecimal.ZERO)
                        .build()).collect(Collectors.toList());
        collect.forEach(f -> {
            yearMonthRevenueStatistics.stream().filter(m -> m.getDateYearMonth().equals(f.getDateYearMonth()))
                    .findFirst().ifPresent(t -> f.setTotalAmount(t.getTotalAmount()));
        });

        return collect;
    }

    @Override
    public ReceiptInflowVO getReceiptInflowData(Date startTime, Date endTime, Long aptId) {
        ReceiptInflowVO vo = overviewTransactionMapper.getReceiptInflowData(startTime != null ?
                DateUtil.beginOfDay(startTime) :
                null, endTime != null ? DateUtil.endOfDay(endTime) : null, aptId);
        //获取交易金额  交易金额与交易明细保持一致
        TotalTransactionDetailsVO totalTransactionDetails =
                overviewTransactionMapper.getTotalTransactionDetails(startTime != null ?
                DateUtil.beginOfDay(startTime) :
                null, endTime != null ? DateUtil.endOfDay(endTime) : null, aptId,
                        TotalTransactionDetailsTypeEnum.TRANSACTIONS.getNode());
        vo.setTotalAmount(totalTransactionDetails.getTotalAmount());
        vo.setOtherAmount(vo.getTotalAmount().subtract(vo.getAccountRecharge()).subtract(vo.getDepositAmount()).subtract(vo.getRentAmount())
                .subtract(vo.getPropertyAmount()));
        return vo;
    }

    @Override
    public TransactionDetailsVO getTransactionDetails(Date startTime, Date endTime, Long aptId) {
        //交易详情
        TransactionDetailsVO transactionDetails = overviewTransactionMapper.getTransactionDetails(startTime != null ?
                DateUtil.beginOfDay(startTime) :
                null, endTime != null ? DateUtil.endOfDay(endTime) : null, aptId);
        PaymentOutflowRatioVO paymentOutflowData = getPaymentOutflowData(startTime, endTime, aptId);
        //覆盖总金额
        paymentOutflowData.setTotalAmount(transactionDetails.getTotalAmount());
        BeanUtil.copyProperties(paymentOutflowData,transactionDetails);
        return transactionDetails;
    }

    /**
     * 获取近一年时间集合
     *
     * @return
     */
    private List<String> getYearMonthList() {
        List<String> yearMonths = new ArrayList<>();
        LocalDate today = LocalDate.now();
        for (int i = 0; i < 12; i++) {
            LocalDate localDate = today.minusMonths(i);
            String yearMonth = localDate.toString().substring(0, 7);
            yearMonths.add(yearMonth);
        }
        //倒序
        Collections.reverse(yearMonths);
        return yearMonths;
    }


}
